var express = require('express');
var router = express.Router();
var pool=require('../pool')
var multer=require('multer')
var fs=require('fs')
var {randomCode,sendCode}=require('../yzm');




// 重设我的形象
router.get('/update',(req,res,next)=>{
  res.send('111111111')
})


// 重设我的形象
router.post('/updatefigure2',(req,res,next)=>{
  let obj=req.body
  let sql='update user set form1=?,form2=?,form3=? where uid=?';
  pool.query(sql,[obj.form1,obj.form2,obj.form3,obj.uid],(err,result)=>{
    if(err){
      next(err);
      return;
    }
    if(result.affectedRows>0){
      res.send({
        code:200,
        msg:'ok',
        data:result
      })
    }else{
      res.send({
        code:201,
        msg:'no'
      })
    }
  })
})
//插入聊天内容
router.post('/up/socket',(req,res,next)=>{
  var obj=req.body
  obj.sid=null;
  let sql='insert into socket set ?';
  pool.query(sql,[obj],(err,result)=>{
    if(err){
      next(err);
      return
    }
    if(result.affectedRows>0){
      res.send({
        code:200,
        msg:'ok',
        data:result
      });
    }else{
      res.send({
        code:201,
        msg:'no'
      });
    }
  })
})

// 一起来聊个天吧
router.post('/el/socket',(req,res)=>{
  let uid=req.body.uid;
  let id=req.body.id
  let sql = `select * from socket where uid="${uid}" and id="${id}"`;
  pool.query(sql, (err, result) => {
    if(err)throw err;
    if(result.length>0){
      res.send({
        code:200,
        msg:'ok',
        data:result
      })
    }else{
      res.send({
        code:201,
        msg:'no',
        data:[]
      })
    }
  });

})


router.post("/upAvatar",multer({
  dest:'upload'//upload表示上传到upload文件夹，如果不存在会自动创建
  }).single('file'),(req,res)=>{//single('file')表示单文件上传，file表示input的name名，这里需要注意，如果有多个input都是上传图片用的，则name都要相同
      var uid=req.body.uid
      //上传了图片
      var fileName=new Date().getTime()+"_"+uid+"_"+req.file.originalname//文件名重命名
      console.log(fileName);
      fs.renameSync(req.file.path,__dirname+"../../upload/"+fileName)//新命名的路径替换掉原来的路径，显示图片
      //保存地址到数据库,每个用户都有一个默认的图片和地址，更新地址就可以
    
      var head="//127.0.0.1:3000/"+fileName
      var sql=`update user set head=? where uid=?`
      pool.query(sql,[head,uid],(err,result)=>{
          if(err) throw err;
          if(result.affectedRows>0){
              res.send({code:200,msg:"成功",data:head})
          }else{
              res.send({code:201})
          }
      })
  })
// 拍照
router.get('/touxiang', (req, res) => {
  let uid=req.query.uid;
  let head=req.query.head;
  let sql = `update user set head="${head}" where uid="${uid}"`;
  pool.query(sql, (error, results) => {
    if (error) throw error;
    res.send({ message: 'ok', code: 200, results: results });
  });
});


function create_token(leng){
  leng = leng==undefined?32:leng	//如果没设置token长度自动为32位
  //预设随机字符串
  let chars = 'ABCDEFGHJKMNPQRSTWXYZabcdefhijkmnprstwxyz23456789'
  let token = ''
  //以长度单位进行循环
  for(let i=0;i<leng;++i){
      token+=chars.charAt(Math.floor(Math.random()*chars.length))
  }
  console.log(token)
  return token
}
var tok=create_token()

//用户登录
router.post('/login',(req,res,next)=>{
    let uname=req.body.uname;
    let upwd=req.body.upwd; 
    var sql='select * from user where uname=? and upwd=?';
    if(req.body.uname){
      pool.query(sql,[uname,upwd],(err,result)=>{
        if(err){
          next(err);
          return;
        }
        if(result.length==1){
          tok=create_token()  
          res.send({
            code:200,
            msg:'ok',
            data:result,
            token:tok
          })
        }else{
          res.send({
            code:201,
            msg:'no',
            data:[]
          })
        }
      })
    }else{
      if(req.body.token==tok){
        res.send({code:200,msg:'ok'})
      }else{
        res.send({code:201,msg:'no'})
      }
    }
    
})

//用户注册
router.post('/res',(req,res,next)=>{
  var obj=req.body;
  obj.uid=null; 
  // console.log(obj)
  var sql='insert into user set ?';
  pool.query(sql,[obj],(err,result)=>{
    if(err){
      next(err);
      return;
    }
    if(result.affectedRows>0){
      res.send({
        code:200,
        msg:'ok',
        data:result
      });
    }else{
      res.send({
        code:201,
        msg:'no'
      });
    }
  }) 
})

//查询用户名是否存在接口1
router.get('/v1/uname',(req,res,next)=>{
  var uname=req.query.uname;
  var sql='select * from user where uname=?';
  pool.query(sql,[uname],(err,result)=>{
    if(err){
      next(err);
      return
    }
    if(result.length>0){
      res.send({
        code:200,
        msg:'ok',
        data:result
      })
    }else{
      res.send({
        code:201,
        msg:'no'
      })
    }
  })
})

//查询用户名是否存在接口2
router.get('/v2/uname',(req,res,next)=>{
  var uname=req.query.uid;
  // console.log(uname);
  var sql='select * from user where uid=?';
  pool.query(sql,[uname],(err,result)=>{
    if(err){
      next(err);
      return
    }
    if(result.length>0){
      res.send({
        code:200,
        msg:'ok',
        data:result
      })
    }else{
      res.send({
        code:201,
        msg:'no'
      })
    }
  })
})

//查询手机号是否存在接口
router.get('/v2/phone',(req,res,next)=>{
  var phone=req.query.phone;
  var sql='select * from user where phone=?';
  pool.query(sql,[phone],(err,result)=>{
    if(err){
      next(err);
      return
    }
    if(result.length>0){
      res.send({
        code:200,
        msg:'ok',
        data:result
      })
    }else{
      res.send({
        code:201,
        msg:'no'
      })
    }
  })
})

//查询昵称是否存在接口
router.get('/v3/user_name',(req,res,next)=>{
  var user_name=req.query.user_name;
  var sql='select * from user where user_name=?';
  pool.query(sql,[user_name],(err,result)=>{
    if(err){
      next(err);
      return
    }
    if(result.length>0){
      res.send({
        code:200,
        msg:'ok',
        data:result
      })
    }else{
      res.send({
        code:201,
        msg:'no'
      })
    }
  })
})

//全部套餐商品接口
router.get('/commodity',(req,res,next)=>{
  let sql='select * from commodity';
  pool.query(sql,(err,result)=>{
    if(err){
      next(err);
      return;
    }
    if(result.length>0){
      res.send({
        code:200,
        msg:'ok',
        data:result
      })
    }else{
      res.send({
        code:201,
        msg:'no'
      })
    }
  })
})

//首页套餐商品按城市查询接口(每次查询8条传0得第一页数据)
router.get('/v1/commodity',(req,res,next)=>{
  var limit=req.query.limit
  limit*=4;
  let city=req.query.city
  let sql='select * from commodity where city=? limit ?,4';
  pool.query(sql,[city,limit],(err,result)=>{
    if(err){
      next(err);
      return;
    }
    if(result.length>0){
      res.send({
        code:200,
        msg:'ok',
        data:result
      })
    }else{
      res.send({
        code:201,
        msg:'no'
      })
    }
  })
})

//首页积分商品接口
router.get('/v2/commodity',(req,res,next)=>{
  let sql='select * from integral limit 5';
  pool.query(sql,(err,result)=>{
    if(err){
      next(err);
      return;
    }
    if(result.length>0){
      res.send({
        code:200,
        msg:'ok',
        data:result
      })
    }else{
      res.send({
        code:201,
        msg:'no'
      })
    }
  })
})

//积分商品页分类接口
router.get('/integral/type',(req,res,next)=>{
  var type=req.query.type;
  var limit=req.query.limit;
  limit*=4;
  var sql='select * from integral where type=? limit ?,4'
  pool.query(sql,[type,limit],(err,result)=>{
    if(err){
      next(err)
      return
    }
    if(result.length>0){
      res.send({
        code:200,
        msg:'ok',
        data:result
      })
    }else{
      res.send({
        code:201,
        msg:'no'
      })
    }
  })
})

//积分商品分类全部接口
router.get('/integral1/type1',(req,res,next)=>{
  var type=req.query.type;
  var sql='select * from integral where type=?'
  pool.query(sql,[type],(err,result)=>{
    if(err){
      next(err)
      return
    }
    if(result.length>0){
      res.send({
        code:200,
        msg:'ok',
        data:result
      })
    }else{
      res.send({
        code:201,
        msg:'no'
      })
    }
  })
})

//套餐详情页接口
router.get('/detail/commodity',(req,res,next)=>{
  let id=req.query.id
  let sql='select * from commodity where id=?';
  pool.query(sql,[id],(err,result)=>{
    if(err){
      next(err);
      return;
    }
    if(result.length>0){
      res.send({
        code:200,
        msg:'ok',
        data:result
      })
    }else{
      res.send({
        code:201,
        msg:'no'
      })
    }
  })
})

//积分商城详情页接口
router.get('/detail/integral',(req,res,next)=>{
  let id=req.query.id
  let sql='select * from integral where id=?';
  pool.query(sql,[id],(err,result)=>{
    if(err){
      next(err);
      return;
    }
    if(result.length>0){
      res.send({
        code:200,
        msg:'ok',
        data:result
      })
    }else{
      res.send({
        code:201,
        msg:'no'
      })
    }
  })
})

//添加地址接口
router.post('/address',(req,res,next)=>{
  var obj=req.body;
  var addr=req.body.addr
  var id=req.body.id
  var sql='insert into address set ?'
  var sql1='select * from address where addr=? and id=?'
  pool.query(sql1,[addr,id],(err,result)=>{
    if(err){
      next(err);
      return
    }
    if(result.length>0){
      res.send({
        code:201,
        msg:'no'
      })
    }else{
      pool.query(sql,[obj],(err,results)=>{
        if(err){
          next(err);
          return
        }
        if(results.affectedRows>0){
          res.send({
            code:200,
            msg:'ok',
            data:results
          });
        }else{
          res.send({
            code:202,
            msg:'no'
          });
        }
      })
    }
  })
  
})

//查询用户的所有保存地址接口
router.get('/sel/address',(req,res,next)=>{
  var id=req.query.id;
  var sql='select * from address where id=?'
  pool.query(sql,[id],(err,result)=>{
    if(err){
      next(err);
      return
    }
    console.log(result)
    if(result.length>0){
      res.send({
        code:200,
        msg:'ok',
        data:result
      })
    }else{
      res.send({
        code:201,
        msg:'no'
      })
    }
  })
})

//套餐订单插入接口
router.post('/v1/package',(req,res,next)=>{
  var obj=req.body;
  obj.id=null;
  var sql='insert into package_1 set ?'
  pool.query(sql,[obj],(err,result)=>{
    if(err){
      next(err);
      return
    }
    if(result.affectedRows>0){
      res.send({
        code:200,
        msg:'ok',
        data:result
      });
    }else{
      res.send({
        code:201,
        msg:'no'
      });
    }
  })
})

//积分商城订单插入接口
router.post('/v2/package',(req,res,next)=>{
  var obj=req.body;
  obj.id=null;
  var sql='insert into package_2 set ?'
  pool.query(sql,[obj],(err,result)=>{
    if(err){
      next(err);
      return
    }
    if(result.affectedRows>0){
      res.send({
        code:200,
        msg:'ok',
        data:result
      });
    }else{
      res.send({
        code:201,
        msg:'no'
      });
    }
  })
})

//用户收藏套餐接口
router.post('/v1/collection',(req,res,next)=>{
  var obj=req.body;
  obj.id=null;
  console.log(obj);
  var sql='insert into conllection_1 set ?'
  pool.query(sql,[obj],(err,result)=>{
    if(err){
      next(err);
      return
    }
    if(result.affectedRows>0){
      res.send({
        code:200,
        msg:'ok',
        data:result
      });
    }else{
      res.send({
        code:201,
        msg:'no'
      });
    }
  })
})

//用户收藏积分商城接口
router.post('/v2/collection',(req,res,next)=>{
  var obj=req.body;
  obj.id=null;
  var sql='insert into conllection_2 set ?'
  pool.query(sql,[obj],(err,result)=>{
    if(err){
      next(err);
      return
    }
    if(result.affectedRows>0){
      res.send({
        code:200,
        msg:'ok',
        data:result
      });
    }else{
      res.send({
        code:201,
        msg:'no'
      });
    }
  })
})

//查询用户是否为vip接口
router.get('/user/vip',(req,res,next)=>{
  var uid=req.query.uid;
  var sql='select vip from user where uid=?';
  pool.query(sql,[uid],(err,result)=>{
    if(err){
      next(err);
      return
    }
    if(result[0].vip=='1'){
      res.send({
        code:200,
        msg:'ok',
      })
    }else{
      res.send({
        code:201,
        msg:'no'
      })
    }
  })
})

//用户修改用户头像接口
router.post('/user/head',(req,res,next)=>{
  var uid=req.body.uid;
  var header=req.body.head
  var sql='update user set head=? where uid=?';
  pool.query(sql,[header,uid],(err,result)=>{
    if(err){
      next(err);
      return
    }
    if(result.affectedRows>0){
      res.send({
        code:200,
        msg:'ok',
        data:result
      });
    }else{
      res.send({
        code:201,
        msg:'no'
      });
    }
  })
})

//查询所有普通头像接口
router.get('/v1/head',(req,res,next)=>{
  var sql='select * from head';
  pool.query(sql,(err,result)=>{
    if(err){
      next(err);
      return
    }
    if(result.length>0){
      res.send({
        code:200,
        msg:'ok',
        data:result
      })
    }else{
      res.send({
        code:201,
        msg:'no'
      })
    }
  })
})

//查询所有vip头像接口
router.get('/v2/vip_head',(req,res,next)=>{
  var sql='select * from vip_head';
  pool.query(sql,(err,result)=>{
    if(err){
      next(err);
      return
    }
    if(result.length>0){
      res.send({
        code:200,
        msg:'ok',
        data:result
      })
    }else{
      res.send({
        code:201,
        msg:'no'
      })
    }
  })
})

//用户购买vip接口
router.post('/user/shop/vip',(req,res,next)=>{
  var uid=req.body.uid;
  var sql="update user set vip='1' where uid=?";
  pool.query(sql,[uid],(err,result)=>{
    if(err){
      next(err);
      return
    }
    if(result.affectedRows>0){
      res.send({
        code:200,
        msg:'ok',
        data:result
      });
    }else{
      res.send({
        code:201,
        msg:'no'
      });
    }
  })
})

//用户修改积分接口
router.post('/user/branch',(req,res,next)=>{
  var uid=req.body.uid;
  var branch=req.body.branch;
  var sql='update user set branch=? where uid=?';
  pool.query(sql,[branch,uid],(err,result)=>{
    if(err){
      next(err);
      return
    }
    if(result.affectedRows>0){
      res.send({
        code:200,
        msg:'ok',
        data:result
      });
    }else{
      res.send({
        code:201,
        msg:'no'
      });
    }
  })
})

//用户修改昵称接口
router.post('/user/name',(req,res,next)=>{
  var uid=req.body.uid;
  var user_name=req.body.user_name;
  var sql='update user set user_name=? where uid=?';
  pool.query(sql,[user_name,uid],(err,result)=>{
    if(err){
      next(err);
      return
    }
    if(result.affectedRows>0){
      res.send({
        code:200,
        msg:'ok',
        data:result
      });
    }else{
      res.send({
        code:201,
        msg:'no'
      });
    }
  })
})

//用户修改抽奖次数接口
router.post('/user/number',(req,res,next)=>{
  var uid=req.body.uid;
  var number=req.body.number;
  var sql='update user set number=? where uid=?';
  pool.query(sql,[number,uid],(err,result)=>{
    if(err){
      next(err);
      return
    }
    if(result.affectedRows>0){
      res.send({
        code:200,
        msg:'ok',
        data:result
      });
    }else{
      res.send({
        code:201,
        msg:'no'
      });
    }
  })
})

//用户增加抽奖次数接口
router.get('/add/lucky',(req,res,next)=>{
  var uid=req.query.uid;
  var sql1='select number from user where uid=?';
  pool.query(sql1,[uid],(err,results)=>{
    if(err){
      next(err);
      return
    }
    var number=results[0].number
    var num=parseInt(number)+1
    var sql='update user set number=? where uid=?';
    pool.query(sql,[num,uid],(err,result)=>{
      if(err){
        next(err);
        return
      }
      if(result.affectedRows>0){
        res.send({
          code:200,
          msg:'ok',
          data:result
        });
      }else{
        res.send({
          code:201,
          msg:'no'
        });
      }
    })
  })
})

//查询用户所有优惠券接口
router.get('/v1/coupon',(req,res,next)=>{
  var id=req.query.id;
  var sql='select * from coupon where id=?'
  pool.query(sql,[id],(err,result)=>{
    if(err){
      next(err);
      return
    }
    if(result.length>0){
      res.send({
        code:200,
        msg:'ok',
        data:result
      })
    }else{
      res.send({
        code:201,
        msg:'no'
      })
    }
  })
})

//插入用户优惠券接口
router.post('/v2/coupon',(req,res,next)=>{
  var obj=req.body;
  var sql='insert into coupon set ?'
  pool.query(sql,[obj],(err,result)=>{
    if(err){
      next(err);
      return
    }
    if(result.affectedRows>0){
      res.send({
        code:200,
        msg:'ok',
        data:result
      });
    }else{
      res.send({
        code:201,
        msg:'no'
      });
    }
  })
})

//查询所有摄影师接口
router.get('/v1/photo',(req,res,next)=>{
  var sql='select * from photo'
  pool.query(sql,(err,result)=>{
    if(err){
      next(err);
      return
    }
    if(result.length>0){
      res.send({
        code:200,
        msg:'ok',
        data:result
      })
    }else{
      res.send({
        code:201,
        msg:'no'
      })
    }
  })
})

//查询全部婚纱接口
router.get('/v1/wed',(req,res,next)=>{
  var sql='select * from wed'
  pool.query(sql,(err,result)=>{
    if(err){
      next(err);
      return
    }
    if(result.length>0){
      res.send({
        code:200,
        msg:'ok',
        data:result
      })
    }else{
      res.send({
        code:201,
        msg:'no'
      })
    }
  })
})

//首页查询部分摄影师接口(查询4条)
router.get('/v2/photo',(req,res,next)=>{
  var limit=req.query.limit
  limit*=4;
  var sql='select * from photo limit ?,4'
  pool.query(sql,[limit],(err,result)=>{
    if(err){
      next(err);
      return
    }
    if(result.length>0){
      res.send({
        code:200,
        msg:'ok',
        data:result
      })
    }else{
      res.send({
        code:201,
        msg:'no'
      })
    }
  })
})

//首页查询部分婚纱接口(查询4条)
router.get('/v2/wed',(req,res,next)=>{
  var limit=req.query.limit
  limit*=4;
  var sql='select * from wed limit ?,4'
  pool.query(sql,[limit],(err,result)=>{
    if(err){
      next(err);
      return
    }
    if(result.length>0){
      res.send({
        code:200,
        msg:'ok',
        data:result
      })
    }else{
      res.send({
        code:201,
        msg:'no'
      })
    }
  })
})

//插入评论接口
router.post('/v1/pinlun',(req,res,next)=>{
  var obj=req.body;
  obj.id=null;
  var sql='insert into comment_1 set ?'
  pool.query(sql,[obj],(err,result)=>{
    if(err){
      next(err);
      return
    }
    if(result.affectedRows>0){
      res.send({
        code:200,
        msg:'ok',
        data:result
      });
    }else{
      res.send({
        code:201,
        msg:'no'
      });
    }
  })
})


//用户上传头像接口
router.post('/h1/header',(req,res,next)=>{
  var obj=req.body;
  obj.id=null;
  var sql='insert into header set ?'
  pool.query(sql,[obj],(err,result)=>{
    if(err){
      next(err);
      return
    }
    if(result.affectedRows>0){
      res.send({
        code:200,
        msg:'ok',
        data:result
      });
    }else{
      res.send({
        code:201,
        msg:'no'
      });
    }
  })
})

//用户查询上传头像
router.get('/h2/header',(req,res,next)=>{
  var uid=req.query.uid;
  var sql='select * from header where uid=?';
  pool.query(sql,[uid],(err,result)=>{
    if(err){
      next(err);
      return
    }
    if(result.length>0){
      res.send({
        code:200,
        msg:'ok',
        data:result
      })
    }else{
      res.send({
        code:201,
        msg:'no'
      })
    }
  })
})

//用户修改昵称接口
router.get('/up/user_name',(req,res,next)=>{
  var user=req.query.user;
  var uid=req.query.uid;
  var sql='update user set user_name=? where uid=?'
  pool.query(sql,[user,uid],(err,result)=>{
    if(err){
      next(err);
      return
    }
    if(result.affectedRows>0){
      res.send({
        code:200,
        msg:'ok',
        data:result
      });
    }else{
      res.send({
        code:201,
        msg:'no'
      });
    }
  })
})

//用户修改姓名接口
router.get('/up/name',(req,res,next)=>{
  var user=req.query.user;
  var uid=req.query.uid;
  var sql='update user set uname=? where uid=?'
  pool.query(sql,[user,uid],(err,result)=>{
    if(err){
      next(err);
      return
    }
    if(result.affectedRows>0){
      res.send({
        code:200,
        msg:'ok',
        data:result
      });
    }else{
      res.send({
        code:201,
        msg:'no'
      });
    }
  })
})

//用户修改手机号接口
router.get('/up/phone',(req,res,next)=>{
  var user=req.query.user;
  var uid=req.query.uid;
  var sql='update user set phone=? where uid=?'
  pool.query(sql,[user,uid],(err,result)=>{
    if(err){
      next(err);
      return
    }
    if(result.affectedRows>0){
      res.send({
        code:200,
        msg:'ok',
        data:result
      });
    }else{
      res.send({
        code:201,
        msg:'no'
      });
    }
  })
})

// 查询摄影套餐收藏接口
router.get('/v1/collections',(req,res,next)=>{
  let uid=req.query.uid
  let lid=req.query.lid
  let sql='select * from conllection_1 where uid=? and lid=?'
  pool.query(sql,[uid,lid],(err,result)=>{
    if(err){
      next(err);
      return;
    }
    if(result.length>0){
      res.send({
        code:200,
        msg:'ok',
        data:result
      })
    }else{
      res.send({
        code:201,
        msg:'no'
      })
    }
  })
})

//查询用户是否评论过该商品接口
router.get('/yz/a_pinlun',(req,res,next)=>{
  var uid=req.query.uid;
  var user_name=req.query.user_name;
  var sql='select * from comment_1 where uid=? and user_name=?';
  pool.query(sql,[uid,user_name],(err,result)=>{
    if(err){
      next(err);
      return
    }
    if(result.length==0){
      res.send({
        code:200,
        msg:'ok',
        data:result
      })
    }else{
      res.send({
        code:'201',
        msg:'no'
      })
    }
  })
})

//查询商品的所有评论接口
router.get('/all/pl',(req,res,next)=>{
  var uid=req.query.uid;
  var sql='select * from comment_1 where uid=?';
  pool.query(sql,[uid],(err,result)=>{
    if(err){
      next(err);
      return
    }
    if(result.length>0){
      res.send({
        code:200,
        msg:'ok',
        data:result
      })
    }else{
      res.send({
        code:'201',
        msg:'no'
      })
    }
  })
})

//查询商品的好评评论接口
router.get('/good/pl',(req,res,next)=>{
  var uid=req.query.uid;
  var sql='select * from comment_1 where uid=? and type>=4';
  pool.query(sql,[uid],(err,result)=>{
    if(err){
      next(err);
      return
    }
    if(result.length>0){
      res.send({
        code:200,
        msg:'ok',
        data:result
      })
    }else{
      res.send({
        code:201,
        msg:'no'
      })
    }
  })
})

//查询商品的中评评论接口
router.get('/center/pl',(req,res,next)=>{
  var uid=req.query.uid;
  var sql='select * from comment_1 where uid=? and type=3';
  pool.query(sql,[uid],(err,result)=>{
    if(err){
      next(err);
      return
    }
    if(result.length>0){
      res.send({
        code:200,
        msg:'ok',
        data:result
      })
    }else{
      res.send({
        code:201,
        msg:'no'
      })
    }
  })
})

//查询商品的差评评论接口
router.get('/low/pl',(req,res,next)=>{
  var uid=req.query.uid;
  var sql='select * from comment_1 where uid=? and type<=2';
  pool.query(sql,[uid],(err,result)=>{
    if(err){
      next(err);
      return
    }
    if(result.length>0){
      res.send({
        code:200,
        msg:'ok',
        data:result
      })
    }else{
      res.send({
        code:201,
        msg:'no'
      })
    }
  })
})

//修改点赞接口
router.get('/pl/a_dz',(req,res,next)=>{
  var id=req.query.id
  var sql='select num from comment_1  where id=?';
  pool.query(sql,[id],(err,result)=>{
    if(err){
      next(err);
      return
    }
    var num=parseInt(result[0].num)+1
    var sql1='update comment_1 set num=? where id=?';
    pool.query(sql1,[num,id],(err,results)=>{
      if(err){
        next(err);
        return
      }
      if(results.affectedRows>0){
        res.send({
          code:200,
          msg:'ok',
          data:results
        });
      }else{
        res.send({
          code:201,
          msg:'no'
        });
      }
    })
  })
})

//取消点赞状态接口
router.get('/pl/de',(req,res,next)=>{
  var id=req.query.id
  var sql='select num from comment_1  where id=?';
  pool.query(sql,[id],(err,result)=>{
    if(err){
      next(err);
      return
    }
    var num=parseInt(result[0].num)
    var number=num-1;
    console.log(number)
    var sql1='update comment_1 set num=? where id=?';
    pool.query(sql1,[number,id],(err,results)=>{
      if(err){
        next(err);
        return
      }
      if(results.affectedRows>0){
        res.send({
          code:200,
          msg:'ok',
          data:results
        });
      }else{
        res.send({
          code:201,
          msg:'no'
        });
      }
    })
  })
})

//查询地址接口
router.get('/se/addre',(req,res,next)=>{
  var id=req.query.id;
  var sql='select * from address where id=?';
  pool.query(sql,[id],(err,result)=>{
    if(err){
      next(err);
      return
    }
    if(result.length>0){
      res.send({
        code:200,
        msg:'ok',
        data:result
      })
    }else{
      res.send({
        code:201,
        msg:'no'
      })
    }
  })
})



























//删除用户收藏套装接口
router.get('/del/collection',(req,res,next)=>{
  var id=req.query.id;
  var sql='delete  from  conllection_1 where  id=?'
  pool.query(sql,[id],(err,result)=>{
    if(err){
      next(err);
      return
    }
    if(result.affectedRows == 0){
   res.send({
        code:201,
        msg:'no',
      })
    }else{
      res.send({
      code:200,
      msg:'ok',
      })
    }
  })
})


//个人套餐订单查询接口 个人套餐订单查询接口 个人套餐订单查询接口 个人套餐订单查询接口 个人套餐订单查询接口 个人套餐订单查询接口
// 这张表uid插入的是用户uid 在用户表是唯一 所以可通过uid去检索该用户的所有订单
router.get('/v1/packages1',(req,res,next)=>{
  var obj=req.query.uid;
  var sql='select * from package_1 where uid= ?'
  pool.query(sql,[obj],(err,result)=>{
    if(err){
      next(err);
      return
    }
    if(result.length>0){
      res.send({
        code:200,
        msg:'ok',
        data:result
      });
    }else{
      res.send({
        code:201,
        msg:'no'
      });
    }
  })
})




//个人积分商城订单查询接口 个人积分商城订单查询接口 个人积分商城订单查询接口 个人积分商城订单查询接口 个人积分商城订单查询接口 
// 这张表uid插入的是用户uid 在用户表是唯一 所以可通过uid去检索该用户的所有订单
router.get('/v2/packages2',(req,res,next)=>{
  var obj=req.query.uid;
  var sql='select * from package_2 where uid= ?'
  pool.query(sql,[obj],(err,result)=>{
    if(err){
      next(err);
      return
    }
    if(result.length>0){
      res.send({
        code:200,
        msg:'ok',
        data:result
      });
    }else{
      res.send({
        code:201,
        msg:'no'
      });
    }
  })
})



//用户收藏积分商城查询接口 用户收藏积分商城查询接口 用户收藏积分商城查询接口 用户收藏积分商城查询接口 用户收藏积分商城查询接口 
router.get('/v2/collections',(req,res,next)=>{
  var obj=req.query.uid;
  var sql='select * from conllection_2 where uid=?'
  pool.query(sql,[obj],(err,result)=>{
    if(err){
      next(err);
      return
    }
    if(result.length>0){
      res.send({
        code:200,
        msg:'ok',
        data:result
      });
    }else{
      res.send({
        code:201,
        msg:'no'
      });
    }
  })
})



//查询用户收藏套装接口
router.get('/collection',(req,res,next)=>{
  var id=req.query.id;
  var sql='select * from conllection_1 where uid=?'
  pool.query(sql,[id],(err,result)=>{
    if(err){
      next(err);
      return
    }
    if(result.length>0){
      res.send({
        code:200,
        msg:'ok',
        data:result
      })
    }else{
      res.send({
        code:201,
        msg:'no',
      })
    }
  })
})



// 搜索套餐查询1 套餐标题
router.post('/commo1',(req,res,next)=>{
  console.log(1);
  var title=req.body.title;
  console.log(req.body);
  var sql=`select * from commodity where title like '%${title}%' `
  pool.query(sql,(err,result)=>{
    if(err){
      next(err);
      return
    }
    console.log(result);
    if(result.length>0){
      res.send({
        code:200,
        msg:'ok',
        data:result
      })
    }else{
      res.send({
        code:201,
        msg:'no',
      })
    }
  })
})

// 搜索套餐查询2 城市
router.post('/commo2',(req,res,next)=>{
  console.log(1);
  var city=req.body.city;
  var sql=`select * from commodity where city like '%${city}%' `
  pool.query(sql,(err,result)=>{
     if(err){
      next(err);
      return
    }
    console.log(result);
    if(result.length>0){
      res.send({
        code:200,
        msg:'ok',
        data:result
      })
    }else{
      res.send({
        code:201,
        msg:'no',
      })
    }
  })
})



// 验证码接口
router.get("/puser", (req, res, next) => {
  console.log(0);
     let uphone = req.query.phone;
     let code = randomCode(6); //生成6位数字随机验证码
     sendCode(uphone, code, function (success) {
       if (success) {
           res.send({
            code:200,
            msg:'ok',
      data:code
       })
     console.log(22);
       } else {
         res.send({
        msg:'短信验证码发送失败',
     code:201
     
     });
     console.log(33);
       }
     });
   });
module.exports = router;
